layui.use(['layer', 'form', 'table', 'admin', 'ax', 'laydate', 'func'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var func = layui.func;

    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        trigger: 'click',
        // max: Feng.currentDate()
    });

    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };
    $("#condition").on("input", function (e) {
        //获取input输入的值
        Notice.search();
    });
    form.on('select(dm)', function (data) {
        Notice.search();
    });
    /**
     * 选中行效果
     */
    table.on('row(noticeTable)', function (obj) {
        $(obj.tr).siblings().css("background-color", "");
        obj.tr.css({"background-color": "#dadad9"});
    });
    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyXd/getDm");
    var languageResult = languageAjax.start();
    $.each(languageResult, function (index, val) {
        $("#dm").append("<option value='" + val.dm + "'>" + val.dm + "</option>");
    });
    form.render('select');
    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            {type: 'checkbox',LAY_CHECKED:true,hide:true},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            {field: 'mm', align: "center", sort: false, title: '门店号', fixed: 'left'},
            {field: 'dm', align: "center", sort: false, title: '店名',  fixed: 'left'},
            {field: 'xm', align: "center", sort: false, title: '客人姓名',},
            {field: 'sex', align: "center", sort: false, title: '性别',},
            {field: 'sjh', align: "center", sort: true, title: '手机号',},
            {field: 'xf', align: "center", sort: false, title: '消费金额',},
            {field: 'sj', align: "center", sort: false, title: '录入时间',},
        ]];
    };

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['ym'] = $("#mm").val();
        queryData['dm'] = $("#dm").val();
        queryData['sjh'] = $("#phone").val();
        queryData['xf'] = $("#price").val();
        queryData['sex'] = $("#sex").val();
        queryData['timeLimit'] = $("#timeLimit").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加通知
     */
    Notice.openAddNotice = function () {
        func.open({
            height: 1000,
            title: '添加巡店沟通',
            content: Feng.ctxPath + '/btyXdgt/btyXdgt_add',
            tableId: Notice.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    Notice.exportExcel = function () {
        //获取导出Excel的数据
        var ajax = new $ax(Feng.ctxPath + "/weiXinApi/listExport");
        var ym = $("#mm").val();
        ajax.set("ym", ym);
        var dm = $("#dm").val();
        ajax.set("dm", dm);
        var sjh = $("#phone").val();
        ajax.set("sjh", sjh);
        var xf = $("#price").val();
        ajax.set("xf", xf);
        var sex = $("#sex").val();
        ajax.set("sex", sex);
        var timeLimit = $("#timeLimit").val();
        ajax.set("timeLimit", timeLimit);
        var result = ajax.start();
        var checkRows = table.checkStatus(Notice.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            var lists=result;
            for (index in lists) {
                var mm = lists[index].mm;
                lists[index].mm = mm + "\t";

            }
            table.exportFile(tableResult.config.id, lists, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/weiXinApi/listMt',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Notice.initColumn(),
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Notice.openAddNotice();
    });
    // 导出excel
    $('#btnExp').click(function () {
        Notice.exportExcel();
    });

});
